package com.kma.ncpractice2013.dao;

import com.kma.ncpractice2013.model.User;
import org.springframework.jdbc.core.RowMapper;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @author Illya
 */

public class UserDAO extends DAO<User>
{

    private final String SQL_INSERT_QUERY = "INSERT INTO USERS(ID,LOGIN,PASSWORD,SALT,NAME,SURNAME,BIRTHDATE,PHONE," + 
                "REGISTRATION_DATE,GROUP_ID,BLOCKED,ACCESS_LEVEL_ID,REGISTRATION_TOKEN)" +
                "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)";
    private final String SQL_UPDATE_QUERY = "UPDATE USERS SET LOGIN = ?, PASSWORD = ?, SALT = ?, NAME = ?,"
            + "SURNAME = ?, BIRTHDATE = ?, PHONE = ?, REGISTRATION_DATE = ?, GROUP_ID = ?, BLOCKED = ?,"
            + "ACCESS_LEVEL_ID = ?, REGISTRATION_TOKEN = ? WHERE ID = ?";
    
    //TODO: pass only compulsory arguments to the constructor
    public void create(User user)
    {
        String sql = SQL_INSERT_QUERY;
        jdbcTemplate.update(sql, new Object[]{user.getId(),user.getLogin(),user.getPassword(),
            user.getSalt(),user.getName(), user.getSurname(),user.getBirthdate(), user.getPhone(),
            user.getRegistrationDate(),user.getGroupId(), user.isBlocked(),user.getAccessLevelId(),
            user.getRegistrationToken()});
    }

    public void update(User user)
    {
        String sql = SQL_UPDATE_QUERY;
        jdbcTemplate.update(sql, new Object[]{user.getLogin(),user.getPassword(),
            user.getSalt(),user.getName(), user.getSurname(),user.getBirthdate(), user.getPhone(),
            user.getRegistrationDate(),user.getGroupId(), user.isBlocked(),user.getAccessLevelId(),
            user.getRegistrationToken()});
    }

    public User getById(int user_id)
    {
        List<User> userList;
        String sql = "SELECT * FROM USERS WHERE ID = " + user_id;
        userList = jdbcTemplate.query(sql, new UserRowMapper());
        return userList.get(0);
    }
    
    public User getByLogin(String login)
    {
        List<User> userList;

        String sql = "SELECT * FROM USERS WHERE LOGIN = ?";
        userList = jdbcTemplate.query(sql, new UserRowMapper(), login);
        return userList.get(0);
    }

    public List<User> getAll()
    {
        String sql = "SELECT  * FROM USERS";
        List<User> userList = jdbcTemplate.query(sql, new UserRowMapper());
        return userList;
    }

    public void deleteById(int user_id)
    {
        String sql = "DELETE FROM USERS WHERE ID = " + user_id;
        jdbcTemplate.update(sql);
    }

    private static final class UserRowMapper implements RowMapper
    {
        @Override
        public Object mapRow(ResultSet rs, int rowNum) throws SQLException
        {
            User user = new User();
            user.setId(rs.getInt("id"));
           // user.setName(rs.getString("name"));
           // user.setSurname(rs.getString("surname"));
            user.setPassword(rs.getString("phone"));
            user.setSalt(rs.getString("salt"));
           // user.setPhone(rs.getString("phone"));
            user.setRegistrationDate(rs.getDate("registration_date"));
           // user.setBirthdate(rs.getDate("birthdate"));
            user.setLogin(rs.getString("LOGIN"));
            user.setGroupId(rs.getInt("group_id"));
            user.setBlocked(rs.getInt("blocked"));
           // user.setAccessLevelId(rs.getInt("access_level_id"));
            user.setRegistrationToken(rs.getString("registration_token"));
            return user;
        }
    }
}
